﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NumericalCalculations
{



    public class Plotter
    {

        public enum PointTypes
        {
            None, //one pixel point
            Point, //bigger point
            Circle,
            XMark,
            Plus,
            Star,
            Square,
            Diamond,
            TriangleDown,
            TriangleUp,
            TriangleLeft,
            TriangleRight,
            Pentagram,
            Hexagram
        }

        public enum LineTypes
        {
            None, //no line
            Solid,
            Dotted,
            DashDot,
            Dashed,
        }

        public enum AxisRangeMode
        {
            Auto,
            Manual,
            Tight,
            Fill,
            IJ,
            XY,
            Equal,
            Image,
            Square,
            Normal,
            Vis3d,
        }

        public enum GridMode
        {
            None,
            Major,
            Minor
        }

        private const int FigureStartIndex = 1;
        private static Plotter _instance;
        public static Plotter Instance
        {
            get { return _instance ?? (_instance = new Plotter()); }
        }


        private readonly Dictionary<int, Figure> _figures;

        public Figure CurrentFigure
        {
            get
            {
                return Figure.HeadFigure ?? GenerateFigure(FigureStartIndex);
            }
        }

        public Plotter()
        {
            _figures = new Dictionary<int, Figure>();
            
        }

        public void SelectFigure(int figureIndex)
        {
            Figure figureWindow;
            if(!_figures.TryGetValue(figureIndex, out figureWindow))
            {
                //Figure with given number does not exist, so we generate new window
                GenerateFigure(figureIndex);
            }
            else
            {
                if(!Object.ReferenceEquals(Figure.HeadFigure, figureWindow))
                {
                    if(figureWindow.NextFigure != null)
                    {
                        figureWindow.NextFigure.PreviousFigure = figureWindow.PreviousFigure;
                    }

                    if(figureWindow.PreviousFigure != null)
                    {
                        figureWindow.PreviousFigure.NextFigure = figureWindow.NextFigure;
                    }

                    Figure.HeadFigure.NextFigure = figureWindow;
                    figureWindow.NextFigure = null;
                    Figure.HeadFigure = figureWindow;
                }
                
                figureWindow.Show();
                figureWindow.BringToFront();

            }
        }

        public Figure GenerateFigure(int index)
        {
            var figure = new Figure { Index = index };
            _figures.Add(index, figure);

            //Handler for deallocating figure when it's window is closed
            figure.FormClosed += (sender, args) => _figures.Remove(index);

            figure.Show();
            figure.BringToFront();

            return figure;
        }

        public int FirstAvailableIndex()
        {
            int i = 1;
            while (_figures.ContainsKey(i))
            {
                i++;
            }
            return i;
        }

        public Figure GetFigure(int index)
        {
            Figure figureWindow;
            _figures.TryGetValue(index, out figureWindow);
            return figureWindow;
        }
    }
}
